/**
 * 字典管理主界面
 * @author linzongxue
 * @create_date 2012-2-15
 */
define(function(){
	var DictMainPanel = Ext.extend(Ext.Panel, {
		title: '字典管理',
		layout: 'border',
		initComponent: function(){
			DictMainPanel.superclass.initComponent.call(Ext.apply(this, {
				tbar: this.createTbar(),
				items: [
				    this.typeGridPanel = cu.create('sys.dict.TypeGridPanel', {region: 'center'}),
				    this.itemGridPanel = cu.create('sys.dict.ItemGridPanel', {region: 'south',collapseMode: 'mini'})
				]
			}));
		},
		refresh: function(){
			this.typeGridPanel.getStore().load();
		},
		changeType: function(dictType){
			this.itemGridPanel.refresh(dictType);
		},
		findDictTypes: function(key){
			var grid = this.typeGridPanel;
			grid.store.each(function(record, index){
				var found = record.get('name').toLowerCase().indexOf(key.toLowerCase()) > -1;
				found = found || (record.get('text').indexOf(key) > -1);
				grid.getView().getRow(index).style.display = (found ? '' : 'none');
			});
		},
		addDictType: function(){
			use('sys.dict.DictTypeWindow', function(Win){
				new Win({
					callback: function(data){
						//新添加的字典类型放在最后一行
						this.typeGridPanel.insertNewRow(data);
					},
					cbScope: this
				}).show();
			}, this);
		},
		modifyDictType: function(){
			var grid = this.typeGridPanel;
			var record = grid.getSelectionModel().getSelected();
			if (!record){
				cu.hint('请选择要修改的字典类型');
				return;
			}
			use('sys.dict.DictTypeWindow', function(Win){
				new Win({
					dictType: record.json,
					callback: function(data){
						for(var p in data){record.set(p, data[p]);} //更新记录
						record.json = data; //更新json
						record.commit(); //去除单元格中的修改标记
						this.changeType(data);
					},
					cbScope: this
				}).show();
			}, this);
		},
		removeDictType: function(){
			var grid = this.typeGridPanel;
			var records = grid.getSelectionModel().getSelections();
			if (records.length == 0){
				cu.hint('请选择要删除的字典类型');
				return;
			}
			var msg = '确定要删除选中的' + records.length + '个字典类型吗？';
			cu.confirm(msg, function(){
				cu.post({
					url: 'sys/dict/deleteDictType.do',
					params: {ids: lu.pluck(records, 'id')},
					success: function(){
						grid.store.remove(records);
						var row = grid.getSelectionModel().getSelected();
						this.changeType(row ? row.json : null);
					},
					scope: this
				});
			}, this);
		},
		createTbar: function(){
			return au.permit([
			    {xtype:'label', text:'字典类型列表', style:'font-weight:bold;margin-right:5px;'},
				new Ext.form.TwinTriggerField({
					trigger1Class: 'x-form-clear-trigger',
	                trigger2Class: 'x-form-search-trigger',
	                hideTrigger1: true,
	                emptyText: '输入名称或标题查找',
	                style:'margin-left:5px;',
	                onTrigger1Click: function(e){
	                	this.setValue(null);
	        			this.triggers[0].hide();
	                	this.ownerCt.ownerCt.refresh();
	                },
	                onTrigger2Click: function(e){
	                	if (this.getValue() == ''){
	                		this.onTrigger1Click(e);
	                	}
	                	else{
	                		this.ownerCt.ownerCt.findDictTypes(this.getValue());
	                		this.triggers[0].show();
	                	}
	                },
	                listeners: {
	                	specialKey: function(field, e){ //回车搜索
	                		if (e.getKey() == e.ENTER) this.onTrigger2Click();
	                	}
	                }
				}),
				'->',
			    {text: '添加', permitId:'sys.dict.addType', handler: this.addDictType, iconCls: 'icon-add',scope: this, tooltip:'添加字典类型'},
			    {text: '修改', permitId:'sys.dict.modifyType', handler: this.modifyDictType, iconCls: 'icon-edit',scope: this, tooltip:'修改字典类型'},
			    {text: '删除', permitId:'sys.dict.removeType', handler: this.removeDictType, iconCls: 'icon-delete',scope: this, tooltip:'删除字典类型'}
			]);
		}
	});
	return DictMainPanel;
});